<?php

namespace app\api\controller;


use app\api\model\Area;
use app\api\model\City;
use app\api\model\Province;

class PublicAll extends Basic
{
    /**
     * 初始化方法，所有子类继承
     */
    protected function initialize()
    {
        parent::initialize();
    }


    public function index()
    {

        echo '小程序（北京）科技有限公司 技术支持';

    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 返回可用的专业列表
     */
    public function can_use_major()
    {
        //实例化数据库
        $major = new \app\api\model\Major();
        $map['status'] = 1;
        $list = $major->where($map)->order('sort')->select();
        if (!$list) {
            json_success([]);
        }
        json_success($list);
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 返回可用的国家
     */
    public function can_use_country()
    {
        //实例化数据库
        $country_info = new \app\api\model\Country();
        $map['status'] = 1;
        $list = $country_info->where($map)->order('sort desc')->select();
        if ($list) {
            $data['list'] = $list;
            json_success($data);
        } else {
            json_success([]);
        }
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 返回可用的学位
     */
    public function can_use_degree()
    {
        //实例化数据库
        $Degree = new \app\api\model\Degree();
        $map['status'] = 1;
        $list = $Degree->where($map)->order('sort')->select();
        if (!$list) {
            json_success([]);
        }
        json_success($list);
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 返回可用的年级
     */
    public function can_use_grade()
    {
        //实例化数据库
        $grade = new \app\api\model\Grade();
        $map['status'] = 1;
        $list = $grade->where($map)->order('sort')->select();
        if (!$list) {
            json_success([]);
        }
        json_success($list);
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 返回可用的院校
     */
    public function can_use_college()
    {
        //实例化数据库
        $college = new \app\api\model\College();
        $map['status'] = 1;
        $list = $college->where($map)->order('sort')->select();
        if (!$list) {
            json_success([]);
        }
        json_success($list);
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 返回国家下的可用的院校
     */
    public function can_use_country_college()
    {
        $country_ids = input('country_ids/a');
        if(!$country_ids){
            json_fail('获取国家id是失败');
        }
        //实例化数据库
        $college = new \app\api\model\College();
        $map['status'] = 1;
        $map['country_id'] = array('in',$country_ids);
        $list = $college->where($map)->order('sort')->select();
        if (!$list) {
            json_success([]);
        }
        json_success($list);
    }



    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 返回可用课程类别
     */
    public function can_use_course_classify()
    {
        //实例化数据库
        $course_classify = new \app\api\model\CourseClassify();
        $map['status'] = 1;
        $list = $course_classify->where($map)->order('sort')->select();
        if (!$list) {
            json_success([]);
        }
        json_success($list);
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 返回可用留学预算
     */
    public function can_use_budget()
    {
        //实例化数据库
        $budget = new \app\api\model\Budget();
        $map['status'] = 1;
        $list = $budget->where($map)->order('sort')->select();
        if (!$list) {
            json_success([]);
        }
        json_success($list);
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 返回可用在校排名
     */
    public function can_use_school_ranking()
    {
        //实例化数据库
        $can_use_school_ranking = new \app\api\model\SchoolRanking();
        $map['status'] = 1;
        $list = $can_use_school_ranking->where($map)->order('sort')->select();
        if (!$list) {
            json_success([]);
        }
        json_success($list);
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 返回省市区
     */
    public function all_province()
    {
        $province = new Province();
        $city = new City();
        $area = new Area();
        $infoList = $province->field('id,province_name')->select();
        if ($infoList) {
            foreach ($infoList as $k => $v) {
                $city_list = $city->where(array('province_id' => $v['id']))->field('id,city_name')->select();
                if ($city_list) {
                    foreach ($city_list as $key => $value) {
                        $value['area_name'] = $area->where(array('city_id' => $value['id']))->field('id,area_name')->select();
                        $city_list[$key] = $value;
                    }
                }
                $v['city_list'] = $city_list;
                $infoList[$k] = $v;
            }
        }
        json_success($infoList);
    }


}
